﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EyeGuard
{
	public partial class SettingForm : Form
	{
		private const string registerSubKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

		public SettingForm()
		{
			InitializeComponent();
		}

		private void SettingForm_Load(object sender, EventArgs e)
		{
			workingTimeComboBox.Text = Properties.Settings.Default.workingTime.ToString();
			relaxTimeComboBox.Text = Properties.Settings.Default.relaxTime.ToString();
			dailyLimitTimeComboBox.Text = Properties.Settings.Default.dailyLimitTime.ToString();
			delayRelaxTimeComboBox.Text = Properties.Settings.Default.delayRelaxTime.ToString();
			
			ForceRestCheckBox.Checked = Properties.Settings.Default.forceRestFlag;
			StartupCheckBox.Checked = IsSetStartUp();
		}

		private void SaveButton_Click(object sender, EventArgs e)
		{
			var workingTime = Convert.ToInt32(workingTimeComboBox.Text);
			var relaxTime = Convert.ToInt32(relaxTimeComboBox.Text);
			var dailyLimitTime = Convert.ToInt32(dailyLimitTimeComboBox.Text);
			var delayRelaxTime = Convert.ToInt32(delayRelaxTimeComboBox.Text);
			var forceRestFlag = Convert.ToBoolean(ForceRestCheckBox.Checked);

			Properties.Settings.Default.workingTime = workingTime;
			Properties.Settings.Default.relaxTime = relaxTime;
			Properties.Settings.Default.dailyLimitTime = dailyLimitTime;
			Properties.Settings.Default.delayRelaxTime = delayRelaxTime;
			Properties.Settings.Default.forceRestFlag = forceRestFlag;
			Properties.Settings.Default.Save();

			this.Close();
		}

		private void NoopButton_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void StartupCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			var isChecked = StartupCheckBox.Checked;
			SetStartUp(isChecked);
		}

		private Microsoft.Win32.RegistryKey GetRKey()
		{
			return Microsoft.Win32.Registry.CurrentUser.OpenSubKey(registerSubKey, true);
		}

		private void GetRKeyAndPath(ref string keyName, ref string path)
		{
			path = Application.ExecutablePath;
			keyName = path.Substring(path.LastIndexOf("\\") + 1);
			keyName = keyName.Substring(0, keyName.LastIndexOf("."));
		}

		private bool IsSetStartUp()
		{
			var keyName = "";
			var path = "";
			var Rkey = GetRKey();
			GetRKeyAndPath(ref keyName, ref path);
			var value = Rkey.GetValue(keyName, "");
			return (value.ToString() != "");
		}

		private void SetStartUp(bool flag)
		{
			var keyName = "";
			var path = "";
			var Rkey = GetRKey();
			GetRKeyAndPath(ref keyName, ref path);

			if (flag)
			{
				if (Rkey == null)
				{
					Rkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(registerSubKey);
				}
				Rkey.SetValue(keyName, path);
			}
			else
			{
				if (Rkey != null)
				{
					Rkey.DeleteValue(keyName, false);
				}
			}
		}

		private void ForceRestCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			// nothing to do
		}
	}
}
